// Copyright (c) 2009 Christopher Keith (chris.keith@gmail.com)
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

// Demonstrate simple command lookup in C#.
// See usage() below.

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

namespace WindowsApplication1 {
    class Dscl {
        private void usage() {
		    String[] msgs = {
			    "This program demonstrates simple command lookups.",
			    "Run as \"dscl < testdata.txt\".",
		    };
		    foreach (String s in msgs) {
			    System.Console.Out.WriteLine(s);
		    }
	    }
        private void doExit(string msg) {
            usage();
            System.Console.Out.WriteLine(msg);
            System.Environment.Exit(1);
        }
        private void reverse(string[] args) {
            for (int i = args.Length - 1; i >= 0; i--)
                System.Console.Out.Write(args[i] + " ");
            System.Console.Out.WriteLine("");
        }
        private void doprint(string[] args) {
            foreach (String s in args) {
                System.Console.Out.Write(s + " ");
            }
            System.Console.Out.WriteLine("");
        }
        private void HandleCmd(string cmd) {
            if (cmd.Length > 0) {
                string[] words = cmd.Split(cmd[0]);
                BindingFlags b = BindingFlags.NonPublic | BindingFlags.Instance;
                MemberInfo[] mbrInfoArray = GetType().FindMembers(MemberTypes.Method, b,
                    Type.FilterName, words[1]);
                if (mbrInfoArray.Length == 0) {
                    doExit("No method found for cmd: " + cmd);
                }
                string[] args = new string[words.Length - 2];
                for (int i = 2; i < words.Length; i++) {
                    args[i - 2] = words[i];
                }
                Object[] arguments = new Object[1];
                arguments[0] = args;
                GetType().InvokeMember(words[1],
                    b | BindingFlags.InvokeMethod | BindingFlags.ExactBinding,
                        null, this, arguments);
            }
        }
        public void Main() {
            try {
                String cmd;
                while ((cmd = System.Console.ReadLine()) != null) {
                    HandleCmd(cmd);
                }
            } catch (Exception ex) {
                doExit(ex.ToString());
            }
        }
    }
    static class Program {
        [STAThread]
        static void Main() {
            Dscl d = new Dscl();
            d.Main();
        }
    }
}
